<?php
/**
 * @author Nguyễn Sơn Tùng <sontung.x305@gmail.com> <YM :terminator_nst>
 * @version 1.0
 * @copyright fteam.vn
 * Date: 24/04/2012
 * Time: 16:36
 */
class FUser extends RWebUser {
    /**
     * Function to check from Before Login if it is from Cookie
     * @param type $id         | mixed	 | the user ID. This is the same as returned by getId().
     * @param type $states     | array	 | a set of name-value pairs that are provided by the user identity.
     * @param type $fromCookie | boolean | whether the login is based on cookie
     * @return type            | boolean | whether the user should be logged in
     */
    public function beforeLogin($id, $states, $fromCookie)
    {
        if($fromCookie)
        {
            if(empty($states['autoLoginToken']))
            {
                return false;
            }
            $autoLoginKey=$states['autoLoginToken'];
            $db=Yii::app()->db;
            $command=$db->createCommand('SELECT * FROM {{autologin_tokens}} WHERE user_id=:user_id');
            $command->bindValue(':user_id',$id,PDO::PARAM_STR);
            $row=$command->queryRow();
            return !empty($row) && $row['token']===$autoLoginKey;
        }
        return true;
    }

    /*
     * Login Function
     *
     * @param UserIndentity $identity
     * @param int $duration
     */
    public function login($identity, $duration=3600)
    {
        return parent::login($identity, $duration);

    }

    /*
     * Logout Function
     * @param boolean $destroySession destroy the session or not
     */
    public function logout($destroySession= true)
    {
        // I always remove the session variable model.
        Yii::app()->getSession()->remove('current_user');
        Yii::app()->session->clear();
        Yii::app()->session->destroy();

        parent::logout();
    }

    /*
     * Actions to be taken after logging in.
     * Overloads the parent method in order to mark superusers.
     * @param boolean $fromCookie whether the login is based on cookie.
     */
    public function afterLogin($fromCookie)
    {
        parent::afterLogin($fromCookie);
        // Mark the user as a superuser if necessary.

        //Get the user from the CActiveRecord
        //$user=User::model()->findByPk($this->getId());

        $command = Yii::app()->db->createCommand();
        $command->select('id,username,display_name,email,status,recent_login,avatar')->from('{{user}} u')
            ->where('id='.(int)$this->getId())
            ->limit(1);
        $user=$command->queryRow();

        //Add only some neccessary field
        if($user){
            Yii::app()->getSession()->remove('current_user');
            Yii::app()->getSession()->add('current_user',$user);
            if( Rights::getAuthorizer()->isSuperuser($this->getId())===true )
                $this->isSuperuser = true;
        } else {
            throw new CHttpException(503,t('Error while Logging into your account. Please try again later.','flogin'));
        }


    }
}